<template>
  <div class="mod-folder-table">
    <avue-crud ref="crud"
               :page="page"
               :data="dataList"
               :table-loading="dataListLoading"
               :option="tableOption"
               @search-change="searchChange"
               @selection-change="selectionChange">
      <template slot="menuLeft">
        <el-button type="danger"
                   @click="deleteHandle()"
                   size="small":disabled="dataListSelections.length <= 0">批量删除</el-button>
        <el-button type="success"
                   @click="accreditHandle()"
                   size="small":disabled="dataListSelections.length <= 0">批量授权</el-button>
        <file-upload @refreshDataList="init" :folderId ="folderId" v-if="folderId != 0"></file-upload>
      </template>

      <template slot-scope="scope"
                slot="fileSize">
                {{bytesToSize(scope.row.fileSize)}}
      </template>

      <template slot-scope="scope"
                slot="uploadType">
        <el-tag v-if="scope.row.uploadType === 1"
                size="small"
                type="success">研发</el-tag>
        <el-tag v-else
                size="small">储备</el-tag>
      </template>

      <template slot-scope="scope"
                slot="status">
        <el-tag v-if="scope.row.status === 0"
                size="small"
                type="danger">未审核</el-tag>
        <el-tag v-else
                size="small">已审核</el-tag>
      </template>

      <template slot-scope="scope"
                slot="menu">
        <el-button type="success"
                   size="small"
                   icon="el-icon-help"
                   v-if="scope.row.status === 1"
                   @click="accreditHandle(scope.row.id)">授权</el-button>
        <el-button type="success"
                   size="small"
                   icon="el-icon-paperclip"
                   v-if="scope.row.status === 0"
                   @click="auditHandle(scope.row.id)">审核</el-button>
        <el-button type="primary"
                   size="small"
                   icon="el-icon-download"
                   @click="downloadFile(scope.row.fileUrl)">下载</el-button>
        <el-button type="danger"
                   icon="el-icon-delete"
                   size="small"
                   @click.stop="deleteHandle(scope.row.id)">删除</el-button>
      </template>
    </avue-crud>
    <folder-auth ref="folderAuth" @clearSelection="clearSelection"></folder-auth>
  </div>
</template>

<script>
import { tableOption } from '@/crud/course/file'
import FileUpload from '@/components/file-upload'
import FolderAuth from './file-table-auth'
export default {
  data () {
    return {
      dataList: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      dataListSelections: [],
      dataListLoading: false,
      tableOption: tableOption
    }
  },
  components: {
    FileUpload,
    FolderAuth
  },
  props: {
    folderId: '',
    folderName: ''
  },
  created () {
  },
  mounted () {
  },
  methods: {
    init () {
      this.tableOption.title = this.folderName
      this.page = {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      }
      this.getDataList(this.page)
    },
    getDataList (page, params) {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/admin/folder/file/page/' + this.folderId),
        method: 'get',
        params: this.$http.adornParams(Object.assign({
          current: page == null ? this.page.currentPage : page.currentPage,
          size: page == null ? this.page.pageSize : page.pageSize
        }, params))
      }).then(({ data }) => {
        this.dataList = data.records
        this.page.total = data.total
        this.dataListLoading = false
      })
    },
    // 下载
    downloadFile (url) {
      window.open(url, '_blank');
    },
    // 删除和批量删除
    deleteHandle (id) {
      let prodIds = []
      if (id) {
        prodIds.push(id)
      } else {
        prodIds = this.getSeleProdIds()
      }
      this.$confirm('确定进行删除操作?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/admin/folder/file/del'),
          method: 'delete',
          data: this.$http.adornData(prodIds, false)
        }).then(({ data }) => {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.clearSelection()
              this.getDataList(this.page)
            }
          })
        })
      }).catch(() => { })
    },
    searchChange (params) {
      this.getDataList(this.page, params)
    },
    // 多选变化
    selectionChange (val) {
      this.dataListSelections = val
    },
    // 清空多选变化
    clearSelection (){
      this.$refs.crud.toggleSelection()
      this.dataListSelections = []
    },
    // 获取选中的Id列表
    getSeleProdIds () {
      return this.dataListSelections.map(item => {
        return item.id
      })
    },
    // byte转换
    bytesToSize(bytes) {
      if (bytes === 0) return '0 B';
      var k = 1024, // or 1024
          sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
          i = Math.floor(Math.log(bytes) / Math.log(k));

      return (bytes / Math.pow(k, i)).toPrecision(3) + ' ' + sizes[i];
    },
    // 授权
    accreditHandle (id) {
      let prodIds = []
      if (id) {
        prodIds.push(id)
      } else {
        prodIds = this.getSeleProdIds()
      }
      this.$nextTick(() => {
        this.$refs.folderAuth.init(prodIds)
      })
    },
    // 审核
    auditHandle (id) {
      this.$confirm('确定进行审核操作?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'info'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/admin/folder/file/check/' + id),
          method: 'post',
          data: this.$http.adornData()
        }).then(({ data }) => {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.getDataList(this.page)
            }
          })
        })
      })
    }
  }
}
</script>

<style lang="scss">
.avue-crud__title{
  margin: 0;
}
</style>
